// 资产库可用资产信息
var purchaseProductDatas = new Array();
// 采购记录
var proc1Columns = [{
    id: 'commodityCateName',
    title: '类目',
    width: 73,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'commodityClassName',
    title: '类别',
    width: 73,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'name',
    title: '名称',
    width: 73,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'descAndSpec',
    title: '品名及规格',
    width: 200,
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'quantity',
    title: '数量',
    width: 100,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'estimatedAmount',
    title: '预计金额',
    width: 150,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '￥' + value + '元';
        return content;
    }
}, {
    id: 'remark',
    title: '备注',
    type: 'string',
    columnClass: 'text-left',
}, {
    id: 'operation',
    title: '操作',
    width: 130,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        content += '<button class="btn btn-xs btn-info" onclick="editPurchase(\'' + record.id + '\');"><i class="fa fa-edit"></i>  编辑</button>';
        content += '  ';
        content += '<button class="btn btn-xs btn-danger" onclick="deletePurchase(\'' + record.id + '\');"><i class="fa fa-trash-o"></i>  删除</button>';
        return content;
    }
}];

function editPurchase(id) {

    for (var i in purchaseProductDatas) {
        if (purchaseProductDatas[i].id == id) {
            $$scope.applyItem = purchaseProductDatas[i];
            $$scope.$apply();
            $('.hdproc').show();
            return
        }
    }
}
var purchaseId = '';

function isSamePurchase(element) {
    return element.id != purchaseId;
}

function deletePurchase(id) {
    `layer.confirm('确认删除此数据？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        purchaseId = id;
        purchaseProductDatas = purchaseProductDatas.filter(isSamePurchase);
        purchaseGrid.exhibitDatas = purchaseProductDatas;
        purchaseGrid.refresh(false);
        layer.msg('删除成功');
    });`
   
}
var priceOption = {
    lang: 'zh-cn',
    ajaxLoad: false,
    extraWidth: 20,
    // loadURL:'url',
    exportFileName: '采购比价单列表信息',
    datas: purchaseProductDatas,
    columns: proc1Columns,
    gridContainer: 'proc1Container',
    toolbarContainer: 'priceToolBarContainer',
    tools: '',
    pageSize: 10,
    pageSizeLimit: [10, 15, 20]
};
var purchaseGrid = $.fn.dlshouwen.grid.init(priceOption);
purchaseGrid.load();

// 资产库借货记录
var library1Columns = [
    //     {
    //     id: 'number',
    //     title: '序号',
    //     width: 45,
    //     type: 'string',
    //     columnClass: 'text-center',
    // }, 
    {
        id: 'asset_style1',
        title: '资产类目',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'asset_style2',
        title: '资产类别',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'asset_id',
        title: '资产编号',
        width: 170,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'comments',
        title: '资产描述',
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'asset_status',
        title: '状态',
        width: 75,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'create_user_id',
        title: '归属人',
        width: 75,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'pro_name',
        title: '归属项目',
        width: 75,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'city_location',
        title: '放置地',
        width: 75,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'operation',
        title: '操作',
        width: 50,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += '<button class="btn btn-xs btn-danger" onclick="deleteProduct(\'' + record.asset_id + '\');"><i class="fa fa-trash-o"></i>  删除</button>';
            return content;
        }
    }
];


var productDatas = new Array();

function productListInit(productDatas) {
    var library1Option = {
        lang: 'zh-cn',
        ajaxLoad: false,
        extraWidth: 20,
        // loadURL:'url',
        exportFileName: '资产库借货信息',
        datas: productDatas,
        columns: library1Columns,
        gridContainer: 'library1Container',
        toolbarContainer: 'library1ToolBarContainer',
        tools: '',
        pageSize: 10,
        pageSizeLimit: [10, 15, 20]
    };

    var library1Grid = $.fn.dlshouwen.grid.init(library1Option);
    library1Grid.load();
    return library1Grid;
}
var library1Grid = productListInit(productDatas);

var lidColumns = [{
    id: 'asset_id',
    title: '资产编号',
    width: 170,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'city_location',
    title: '放置地',
    width: 75,
    type: 'string',
    columnClass: 'text-center',
}, {
    id: 'comments',
    title: '资产描述',
    type: 'string',
    columnClass: 'text-left',
    // hideType:'lg|md|sm|xs',
}, {
    id: 'operation',
    title: '操作',
    width: 60,
    type: 'string',
    columnClass: 'text-center',
    resolution: function (value, record, column, grid, dataNo, columnNo) {
        var content = '';
        for (var i in library1Grid.exhibitDatas) {
            if (library1Grid.exhibitDatas[i].asset_id == record.asset_id) {
                content += '<button class="btn btn-xs btn-info" onclick="applyProduct(\'' + record.asset_id + '\',this);" disabled>已申请</button>';
                return content;
            }
        }
        content += '<button class="btn btn-xs btn-info" onclick="applyProduct(\'' + record.asset_id + '\',this);"><i class="icon-ok"></i>  申请</button>';
        return content;
    }
}];

var productSelectDatagrid = $CertusJsonAjax.dataInit("findAssetsList", { code: 9 }, lidColumns, 'lidContainer', 'lidToolBarContainer', 5, [10]);

$$scope.searchProduct = function () {
    productSelectDatagrid.parameters = {
        code: 9,
        param: $$scope.select.productType
    };
    productSelectDatagrid.refresh(true);
}

function applyProduct(recordString, me) {
    var exhibitDatas = productSelectDatagrid.exhibitDatas;

    for (var i in exhibitDatas) {
        if (exhibitDatas[i].asset_id == recordString) {

            var flag = true;
            for (var j in productDatas) {
                if (productDatas[j].asset_id == recordString) {
                    flag = false;
                }
            }
            if (flag) {
                productDatas.push(exhibitDatas[i]);
            }
            library1Grid.exhibitDatas = productDatas;
            $(me).html("").html('已申请');
            library1Grid.reload(false);
        }
    }

}

function isSamea(element) {
    return element.asset_id != asset_id;
}
var asset_id = '';

function deleteProduct(productNumber) {
    layer.confirm('确认删除此数据？', {
        btn: ['确认', '取消'] //按钮
    }, function () {
        asset_id = productNumber;
        for (var i in productDatas) {

            if (productDatas[i].asset_id == productNumber) {
                productDatas = productDatas.filter(isSamea)
                library1Grid.exhibitDatas = productDatas;
                library1Grid.reload(false);
            }
        }
        layer.msg('删除成功');
    });

}


// 显示隐藏采购表单
function shproc() {
    $$scope.applyItem = {};
    $$scope.$apply();
    $('.hdproc').show();
}
//隐藏采购表单
function hdproc() {
    $('.hdproc').hide();
}
$$scope.select = {};
$$scope.select.applyForSelect = [{
    name: "商用",
    code: "SY"
},
{
    name: "自用",
    code: "ZY"
},
{
    name: "其他",
    code: "QT"
}
]

$$scope.select.affiliationSelect = [{
    name: "南京",
    code: "STSNJ"
},
{
    name: "北京",
    code: "STSBJ"
},
{
    name: "广州",
    code: "STSGD"
},
{
    name: "上海",
    code: "STSSH"
}
]

$$scope.select.bigClassSelect = [{
    name: "设备",
    code: "SB"
},
{
    name: "服务",
    code: "FW"
},
{
    name: "其他",
    code: "QT"
}
]

$$scope.sbchanged = function (a) {
    if (a == "SB") {
        $$scope.sbshow = true;
        $$scope.sbbushow = false;
    } else {
        $$scope.sbshow = false;
        $$scope.sbbushow = true;
        $$scope.applyItem.commodityCateName = "服务";
        $$scope.applyItem.commodityClassName = "服务";
    }
}

var classSet = {

}
$CertusJsonAjax.ajax("purchaseProductCode", {}, function (res) {
    if (res.data) {
        var cate = res.data.cate;
        var clas = res.data.clas;

        $$scope.select.cateCodeSelect = new Array();
        for (var i in cate) {
            var split = cate[i].split(",");
            $$scope.select.cateCodeSelect.push({
                code: split[0],
                name: split[1]
            })
        }

        $$scope.select.classCodeSelect = new Array();
        for (var i in clas) {
            var classs = clas[i].split(",");
            if (classSet[classs[0]]) {
                classSet[classs[0]].push({
                    code: classs[1],
                    name: classs[2]
                });
            } else {
                classSet[classs[0]] = new Array();
                classSet[classs[0]].push({
                    code: classs[1],
                    name: classs[2]
                });
            }


            $$scope.select.classCodeSelect = {};
        }

    }
})

$$scope.cateChange = function (cateCode) {
    ;
    $$scope.select.classCodeSelect = classSet[cateCode];
}


$CertusFormAjax.ajax("findStyles", {}, function (response) {
    $$scope.select.assetClassSelect = response.data;
});


//资产转移申请 提交
$$scope.applyItemSubmit = function () {
    ;
    if (certusValidateNotnullPolyfill()) {
        var cateCodeSelect = $$scope.select.cateCodeSelect;
        var classCodeSelect = $$scope.select.classCodeSelect;

        for (var i in cateCodeSelect) {
            if (cateCodeSelect[i].code == $$scope.applyItem.commodityCateCode) {
                $$scope.applyItem.commodityCateName = cateCodeSelect[i].name;
            }
        }
        for (var j in classCodeSelect) {
            if (classCodeSelect[j].code == $$scope.applyItem.commodityClassCode) {
                $$scope.applyItem.commodityClassName = classCodeSelect[j].name;
            }
        }

        if ($$scope.applyItem.id) {
            purchaseId = $$scope.applyItem.id;
            purchaseProductDatas = purchaseProductDatas.filter(isSamePurchase);
        } else {
            $$scope.applyItem.id = (new Date()).getTime() + Math.floor(Math.random() * 10);
        }

        purchaseProductDatas.push($$scope.applyItem);
        purchaseGrid.exhibitDatas = purchaseProductDatas;
        purchaseGrid.refresh(false);
        $$scope.applyItem = {};
        $('.hdproc').hide();
    }
}

//请购单 申请 提交

$$scope.submmitPurchase = function () {
    if (certusValidateNotnull()) {

        $CertusJsonAjax.ajax("firstApplyItem", {
            projId: proId,
            buyingRequisitionEntity: $$scope.applyInfo,
            purchaseItems: purchaseGrid.exhibitDatas,
            productApplyItems: library1Grid.exhibitDatas
        }, function (res) {
            if (res.data) {
                if (res.data == "success") {
                    findBacklogPersonHead($CertusFormAjax, "QMQGLC");
                    disBuyingManager1()
                }
            }
            layer.msg(res.data);
        })
    }

}